// pages/myGoods/myGoods.js
import userApi from '../../service/userApi'
const app = getApp()
let codeConfig = app.globalData.codeConfig
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadTips: '上拉加载',
    showPupFlag: false,
    startPage: 1,
    pageSize: 15,
    loading: false,
    data: {},
    goodsId: '', // 订单号
    upAbled: true, // 是否可以上拉加载
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getAsset()
  },
  // 我的宝贝
  getAsset() {
    var that = this
    that.setData({
      loading: true,
      loadTips: '加载中...'
    })
    wx.showLoading({
      title: "加载中"
    })
    var params = {
      startPage: that.data.startPage,
      pageSize: that.data.pageSize
    }
    userApi.getAssetList(params).then(res => {
      wx.stopPullDownRefresh();
      wx.hideLoading()
      that.setData({
        loading: false
      })
      if (res.data.errCode === codeConfig.errCode.SUCCESS_CODE) {
        that.setData({
          data: res.data.data
        })
        if (res.data.data.goodsList.length < that.data.pageSize) {
          that.setData({
            loadTips: '暂无更多数据',
            upAbled: false
          })
        } else {
          that.setData({
            loadTips: '上拉加载'
          })
        }
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    }).catch(e => {
      wx.stopPullDownRefresh();
      that.setData({
        loading: false
      })
    })
  },
  /**
   * 提货按钮
   */
  onPickUpGoods (e) {
    var url = e.currentTarget.dataset.url
    var goods = e.currentTarget.dataset.goods
    wx.navigateTo({
      url: `${url}?goodsId=${goods.goodsId}&goodsName=${goods.goodsName}&goodsCount=${goods.goodsCount}`
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAsset()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      startPage: 1,
      pageSize: 15,
      upAbled: true // 是否可上拉加载
    })
    this.getAsset()
  },

/**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (!that.data.upAbled) {
      return false
    }
    if (that.data.loading) {
      return false
    }
    that.setData({
      loading: true,
      loadTips: '加载中...',
      startPage: that.data.startPage + 1
    })
    var params = {
      startPage: that.data.startPage,
      pageSize: that.data.pageSize
    }
    userApi.getAssetList(params).then(res => {
      that.setData({
        loading: false
      })
      if (res.data.errCode === codeConfig.errCode.SUCCESS_CODE) {
        var arr = that.data.data.goodsList.concat(res.data.data.goodsList)
        that.setData({
          'data.goodsList': arr
        })
        if (res.data.data.goodsList.length < that.data.pageSize) {
          that.setData({
            loadTips: '暂无更多数据',
            startPage: that.data.startPage - 1,
            upAbled: false
          })
        } else {
          that.setData({
            loadTips: '上拉加载'
          })
        }
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    }).catch(e => {
      console.log(e)
      that.setData({
        loading: false
      })
    })
  }
})